<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller as BaseController;
use App\Http\Requests\SetmealRequest;
use App\Http\Responses\CustomResponse;
use App\Services\admin\SetmealService;
use Illuminate\Http\Request;

/**
 * 套餐管理
 * User: zhx
 * Date: 2023/10/31 0031
 * Time: 14:05
 */
class SetmealController extends BaseController
{
    /**
     * 显示列表
     *
     * @Desc:
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     *
     * @author: zhx
     *
     * @Time: 2023/8/9 14:02
     */
    public function index(Request $request)
    {
        $requestAll = $request->all();
        $list = SetmealService::getPageData($requestAll);

        return CustomResponse::success($list);
    }

    /**
     * 保存新创建
     *
     * @Desc:
     *
     * @param  SetmealRequest  $request 验证请求类
     *
     * @author: zhx
     *
     * @Time: 2023/8/10 14:43
     */
    public function store(SetmealRequest $request)
    {

        if ($request->input('id')) {
            // 编辑
            $data = $request->all(['id', 'category', 'name', 'theme_colors', 'fonts_color', 'app_id', 'description', 'introduction', 'numeral', 'year_unit', 'price', 'renewal_fee', 'how', 'state']);
            $result = SetmealService::edit($data['id'], $data);
            if ($result) {
                return CustomResponse::success($result->id, '编辑成功');
            } else {
                return CustomResponse::error('编辑失败');
            }
        } else {
            // 创建
            $data = $request->all(['category', 'name', 'theme_colors', 'fonts_color', 'app_id', 'description', 'introduction', 'numeral', 'year_unit', 'price', 'renewal_fee', 'how', 'state']);
            $result = SetmealService::create($data);
            if ($result) {
                return CustomResponse::success($result->id, '创建成功');
            } else {
                return CustomResponse::error('创建失败');
            }
        }
    }

    /**
     * 删除
     *
     * @Desc:
     *
     * @author: zhx
     *
     * @Time: 2023/8/9 14:03
     */
    public function destroy($id)
    {
        $ids = explode(',', $id);
        $result = SetmealService::destroy($ids);

        return $result ? CustomResponse::ok('删除成功') : CustomResponse::error('删除失败');
    }
}
